﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
namespace eryatest
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (eryaDataContext dc = new eryaDataContext())
            {
                Label1.Text = dc.eryadb.Count().ToString();
            }
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }
        public  void GetListByHtml(string text, string pat)
        {
            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(pat, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match m = r.Match(text);
            int matchCount = 0;
            while (m.Success)
            {
                tempp[matchCount] = m.Value;
                matchCount++;
                m = m.NextMatch();
            }
        }
        string[] tempp=new string[100];
        string[] temppp;
        protected void Button1_Click(object sender, EventArgs e)
        {

                TextBox2.Text = null;   
                string regex = @"(?is)<dt[^>]*>(?><dt[^>]*>(?<o>)|</dt>(?<-o>)|(?:(?!</?dt\b).)*)*(?(o)(?!))</dt>";
                GetListByHtml(TextBox1.Text, regex);
                for (int i = 0; i < 4; i++)
                {
                    temppp = (tempp[i].Split(new char[] { ':', '<', '>', 'd', 't', '1', '2', '、', '&', ';', 'l', 'd', 'q', 'u', 'o', 'n', 'b', 's', 'p', 'r','.','：','。' }, StringSplitOptions.RemoveEmptyEntries));
                    tempp[i] = temppp[0];
                    TextBox2.Text += tempp[i] + Environment.NewLine;
                }
                TextBox2.Text += "--------以上是提交搜索的关键字-----------" + Environment.NewLine; 
                for (int i = 0; i < 4; i++)
                {
                    string strURL = "http://erya.hang.im/search/";
                    string postData = string.Empty;

                    postData += "keyword=" + tempp[i];
                    Encoding utf8 = Encoding.UTF8;
                    byte[] data = utf8.GetBytes(postData);

                    HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                    request.Accept = "text/html, */*; q=0.01";
                    request.Referer = "http://erya.hang.im/";
                    request.Method = "POST";
                    Stream newStream = request.GetRequestStream();
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();
                    request.KeepAlive = true;
                    //request.Headers.Add("Origin", "http://erya.hang.im");
                    //request.Headers.Add("charset", "UTF-8");
                    request.ContentType = "application/x-www-form-urlencoded";
                    //request.Headers.Add("Accept-Encoding", "gzip, deflate,sdch");
                    //request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36";
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                    string backstr = sr.ReadToEnd();
                    string[] temp = (backstr.Split(new char[] { 'p', '<', '>','n','l','i','/','\\','u','\n','\r',' ' }, StringSplitOptions.RemoveEmptyEntries));
                    backstr = null;
                    for (int j = 0; j < temp.Count(); j = j + 2)
                    {
                        backstr += temp[j] + temp[j + 1] + Environment.NewLine;
                        using (eryaDataContext dc = new eryaDataContext())
                        {
                            var isNull = dc.eryadb.Where(o => o.question == temp[j]);
                            if (isNull.Count() == 0)
                            {
                                eryadb ey = new eryadb();
                                ey.question = temp[j];
                                ey.answer = temp[j + 1];
                                dc.eryadb.InsertOnSubmit(ey);
                                dc.SubmitChanges();
                            }
                        }
                    }
                    TextBox2.Text += backstr + Environment.NewLine;
                    TextBox2.Text += "--------以上第" + i + "题-----------" + Environment.NewLine; 
                }
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox2.Text = null;
            string regex = @"(?is)<dt[^>]*>(?><dt[^>]*>(?<o>)|</dt>(?<-o>)|(?:(?!</?dt\b).)*)*(?(o)(?!))</dt>";
            GetListByHtml(TextBox1.Text, regex);
            for (int i = 0; i < 50; i++)
            {
                //temppp = (tempp[i].Split(new char[] { ':', '<', '>', 'd', 't', '1', '2','3','4','5','6','7','8','9','0', '、', '&', ';', 'l', 'd', 'q', 'u', 'o', 'n', 'b', 's', 'p', 'r', '.', '：', '。' }, StringSplitOptions.RemoveEmptyEntries));
                temppp = (tempp[i].Split(new char[] { '、','&' }, StringSplitOptions.RemoveEmptyEntries));
                tempp[i] = temppp[1];
                TextBox2.Text += tempp[i] + Environment.NewLine;
            }
            TextBox2.Text += "--------以上是提交搜索的关键字-----------" + Environment.NewLine;
            for (int i = 0; i < 50; i++)
            {
                string strURL = "http://erya.hang.im/search/";
                string postData = string.Empty;

                postData += "keyword=" + tempp[i];
                Encoding utf8 = Encoding.UTF8;
                byte[] data = utf8.GetBytes(postData);

                HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
                request.Accept = "text/html, */*; q=0.01";
                request.Referer = "http://erya.hang.im/";
                request.Method = "POST";
                Stream newStream = request.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
               
                request.KeepAlive = true;
                //request.Headers.Add("Origin", "http://erya.hang.im");
                //request.Headers.Add("charset", "UTF-8");
                request.ContentType = "application/x-www-form-urlencoded";
                //request.Headers.Add("Accept-Encoding", "gzip, deflate,sdch");
                //request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36";
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                string backstr = sr.ReadToEnd();
                string[] temp = (backstr.Split(new char[] { 'p', '<', '>', 'n', 'l', 'i', '/', '\\', 'u', '\n', '\r', ' ' }, StringSplitOptions.RemoveEmptyEntries));
                backstr = null;
                for (int j = 2; j < temp.Count(); j = j + 2)
                {
                    if (j + 1 < temp.Count())
                    {
                        backstr += temp[j] + temp[j + 1] + Environment.NewLine;

                        using (eryaDataContext dc = new eryaDataContext())
                        {
                            var isNull = dc.eryadb.Where(o => o.question == temp[j]);
                            if (isNull.Count() == 0)
                            {
                                eryadb ey = new eryadb();
                                ey.question = temp[j];
                                ey.answer = temp[j + 1];
                                dc.eryadb.InsertOnSubmit(ey);
                                dc.SubmitChanges();
                            }
                        }
                        TextBox2.Text += backstr + Environment.NewLine;
                        TextBox2.Text += "--------以上第" + i + "题-----------" + Environment.NewLine;
                        
                    }
                    else
                    {
                       
                    }
                    
                }
               
            }
            
        }
    }
}
